﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace lchealthy
{
    public partial class frmInventoryShow : Form
    {
        public frmInventoryShow()
        {
            InitializeComponent();
        }
        DataSet ds;
        private void frmInventoryShow_Load(object sender, EventArgs e)
        {
            string sql = "select [PID],[PName] from ProductInfo ";
            ds = DbHelperOleDb.Query(sql);
            DataRow dr = ds.Tables[0].NewRow();
            dr[1] = "全部";
            dr[0] = -1;
            ds.Tables[0].Rows.InsertAt(dr, 0);
            cmbPname.DataSource = ds.Tables[0];
            cmbPname.DisplayMember = "PName";
            cmbPname.ValueMember = "PID";
            cmbPname.SelectedIndex = 0;
            InventoryBind();
        }

        private void InventoryBind()
        {
            bind();
        }

        private void btnSerach_Click(object sender, EventArgs e)
        {
            bind();
        }

        private void bind()
        {
            string sql1 = "SELECT PName,pprice,inum,pprice*inum as psum ,ProductInfo.PID FROM ProductInfo INNER JOIN inventory ON ProductInfo.PID = inventory.Pid where inum>0 and did=" + userinfo.id;
            int pid = -1;
            if (cmbPname.SelectedIndex != 0)
            {
                pid = (int)cmbPname.SelectedValue;
                sql1 = sql1 + "and  ProductInfo.Pid=" + pid;
            }
            ds = DbHelperOleDb.Query(sql1);
            decimal a = 0,s=0;
            DataRow dr= ds.Tables[0].NewRow();
            dr[0] = "总计：";
            for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
            {
                a += Convert.ToDecimal(ds.Tables[0].Rows[i][3]);
                s += Convert.ToDecimal(ds.Tables[0].Rows[i][2]);
            }
            dr[3] = a;
            dr[2] = s;
            ds.Tables[0].Rows.Add(dr);
            dgvInventory.AutoGenerateColumns = false;
            dgvInventory.DataSource = ds.Tables[0];
        }

        private void btnEdite_Click(object sender, EventArgs e)
        {
            int row = -1;
            row = dgvInventory.CurrentCell.RowIndex;
            int id = int.Parse(ds.Tables[0].Rows[row]["PID"].ToString());
            frmInventory f = new  frmInventory(id);
            f.ShowDialog();           
            InventoryBind();
        }

        private void btnRefres_Click(object sender, EventArgs e)
        {
            InventoryBind();
        }

        private void btnin_Click(object sender, EventArgs e)
        {
            frmInventory f = new frmInventory();
            f.ShowDialog();
            InventoryBind();
        }

        private void btnOut_Click(object sender, EventArgs e)
        {
            myfun.DataToExcel(dgvInventory);
        }

        private void btnPrint_Click(object sender, EventArgs e)
        {
            myprint m = new myprint(dgvInventory);
            m.Print();
        }
    }
}
